<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Library\COM;

/**
 * Description of OrderDo
 *
 * @author gangguo
 */
class OrdersDo {
    //put your code here
    
    static public function getMyObject(){
         P(get_instance_of(__CLASS__));
    }


    static public function getOrders(){
        return D('Common/Orders');
    }
    
    static public function checkOrderId($order_id){
        return self::getOrders()->checkId($order_id);
    }
    
    static public function getOrderList($member_id){
        return self::getOrders()->getDataById($member_id);
    }
    
    static public function createOrder($data,$pos,$memberInfo){
        $Orders = self::getOrders();
        $Items = D('Common/OrderItems');
        $Orders->startTrans();
        $res1 = $Orders->SaveData($data);
        $res2 = $Items->SaveData($pos);
        foreach($pos as $key=>$val){
            $cart_ids[$key] = $val['cart_id'];
            $goods_ids[$key] = $val['goods_id'];
        }
//        $payment['payment_id'] = date('YmdHis',time()).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
//        $payment['money'] = $data['payed'];
//        $payment['cur_money'] = $data['payed'];
//        $payment['member_id'] = $memberInfo['member_id'];
//        $payment['pay_name'] = '微信JSAPI支付';
//        $payment['op_id'] = 'system';
//        $payment['t_begin'] = time();
//        $payment['payment_bn'] = $data['order_id'];
//        $payment['pay_app_id'] = 'wxpay';
//        $payment['ip'] = get_client_ip();
//        $res3 = D('Common/Payments')->SaveData($payment);
        
        if($res1 && $res2){
            $data_log['result'] = 'SUCCESS';
            $data_log['log_text'] = '订单创建成功!';
            D('Common/Members')->addOrderNum($memberInfo['member_id']);//增加会员订单数量
            D('Commom/Cart')->delete(implode(',', $cart_ids));//清空购物车
            \Common\Library\ORG\GoodsDo::store($goods_ids);//减少货品数量;
            $Orders->commit();
            $res = TRUE;
        }else{
            $data_log['result'] = 'FAILURE';
            $data_log['log_text'] = '订单创建失败!';
            $Orders->rollback();
            $res = FALSE;
        }
        $data_log['order_id'] = $data['order_id'];
        $data_log['op_id'] = $memberInfo['member_id'];
        $data_log['op_name'] = $memberInfo['name'];
        $data_log['alttime'] = time();
        $data_log['bill_type'] = 'order';
        $data_log['behavior'] = 'creates';
        $data_log['addon'] = json_encode($data);
        \Think\Log::write('创建订单:'.$data['order_id'].':'.$data_log['log_text']);
        return D('Common/OrderLog')->add($data_log);
        
        
    }
    
    
    public static function surePay($result){
        $Orders = D('Common/Orders');
        $payed = $Orders->GetDataById($result['out_trade_no'], 'payed,member_id');
        //支付流水处理
        $Payments = D('Common/Payments');
        $pppwhere = " AND `payment_bn` = '{$result['out_trade_no']}' AND `status` = 'ready' AND `pay_app_id` = 'wxpay'";
 
        $paym_ =  $Payments->GetDataByWhere($pppwhere,'',0,1,'payment_id,money');
        $price = $paym_[0][0]['money'];
        $payment = array();
        $order_d = array();
        if (($price * 100) != $result['total_fee'] || ($payed['payed'] * 100) != $result['total_fee']) {
            \Think\Log::write('Pay-callback:支付金额异常：' . var_export($result, 1));
            $payment['status'] = 'error';
            $payment['disabled'] = 'true';
            $payment['memo'] = '支付金额异常@'.get_client_ip().'|@|'. json_encode($result);
            $order_d['pay_status'] = '2';
            $order_d['extend'] = '支付金额于订单的金额不符,ip:'.get_client_ip();
        } else {
            $order_d['pay_status'] = '1';
            $payment['status'] = 'succ';
            //会员预存款日志表
            $advs['member_id'] = $payed['member_id'];
            $advs['money'] = $price;
            $advs['message'] = '微信支付';
            $advs['mtime'] = time();
            $advs['payment_id'] = $paym_[0][0]['payment_id'];
            $advs['order_id'] = $result['out_trade_no'];
            $advs['paymethod'] = 'wxpay';
            $advs['memo'] = '订单付款,微信支付';
            $advs['import_money'] = $price['money'];
            $advs['explode_money'] = $price['money'];
            $MemberAdvacne = D('Common/MemberAdvance');
            $member_advance = $MemberAdvacne->getMemberAdvance($Mpam['member_id']);
            $shop_advance = $MemberAdvacne->getShopAdvance();
            $advs['member_advance'] = $member_advance;
            $advs['shop_advance'] = $shop_advance;
            D('Common/MemberAdvance')->SaveData($advs);
            //会员订单数处理
            D('Common/Members')->addOrderNum($Mpam['member_id']);
            //分层处理--红包处理
//            $Redpacket = D('Common/Redpacket');
//            $packet_id = $Redpacket->getRedpacket($Mpam['member_id']);
//            if ($Redpacket->IsExpiration($packet_id)) {
//                $Redpacket->UsePacketN($packet_id);
//            } else {
//                \Think\Log::write('pay-Redpacket:红包过期:packet_id：' . $packet_id . var_export($Mpam, 1));
//            }
        }

        //订单支付状态处理
        $order_d['payment'] = 'wxpay';
        $order_d['order_id'] = $result['out_trade_no'];
        $order_d['last_modified'] = time();
        $order_d['pay_time'] = time();
        $order_d['transaction_id'] = $result['transaction_id'];
        $Orders->SaveData($order_d);
       
        $payment['payment_id'] = $paym_[0][0]['payment_id'];
        $payment['t_payed'] = time();
        $payment['t_confirm'] = time();
        $payment['bank'] = $result['bank_type'];
        $payment['trade_no'] = $result['transaction_id'];

        return $Payments->SaveData($payment);
    }

    

}
